package com.weiweiqin.service.impl;

import com.weiweiqin.dao.PaylogDao;
import com.weiweiqin.model.Customer;
import com.weiweiqin.model.Paylog;
import com.weiweiqin.service.CustomerService;
import com.weiweiqin.service.PaylogService;
import com.weiweiqin.utils.BigDemicalUtil;
import com.weiweiqin.vo.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class PaylogServiceImpl implements PaylogService {
	@Autowired
	private PaylogDao paylogDao;

	@Autowired
	private CustomerService customerService;

	public List<Paylog> pageList(Page page, Map<String, Object> conditions) {
		return paylogDao.pageList(page, conditions);
	}

	public int totalCount(Map<String, Object> conditions) {
		return paylogDao.totalCount(conditions);
	}

	public void save(Paylog paylog) {
		paylogDao.insert(paylog);
		
	}

	public void update(Paylog paylog) {
		paylogDao.update(paylog);
	}

	@Transactional
	public void reCharge(Paylog paylog) {
		update(paylog);
		Customer customer = customerService.get(paylog.getCustomerId());
		double money = BigDemicalUtil.add(customer.getAmount().doubleValue(), paylog.getPrice().doubleValue());
		customer.setAmount(new BigDecimal(money));
		customerService.update(customer);
	}

	public Paylog get(int id) {
		return paylogDao.get(id);
	}

	@Override
	public Paylog getByOrderNo(String orderNo) {
		return paylogDao.getByOrderNo(orderNo);
	}


}